package com.itboye.mengwu.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.AllData;
import com.itboye.mengwu.data.model.ShopCarXiangQing;
import com.itboye.mengwu.data.model.GoodsCommentsModel.GoodsComments;
import com.itboye.mengwu.data.model.ShopCarNext;

/** GirdView 数据适配器 */
public class GoodsCommentAdapter extends BaseAdapter {

	private Context mContext;
	private ShopCarXiangQing items;

	public GoodsCommentAdapter(Context ctx, ShopCarXiangQing items) {
		this.mContext = ctx;
		this.items = items;
	}

	@Override
	public int getCount() {
		return items.getList() == null ? 0 : items.getList().size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext,
					R.layout.item_goodscomment_listviw, null);
			holder.iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.tv_content = (TextView) convertView
					.findViewById(R.id.tv_content);
			holder.gridview = (NoScrollGridView) convertView
					.findViewById(R.id.gridview);
			holder.imgZan = (TextView) convertView.findViewById(R.id.imgZan);
			holder.tetPing = (TextView) convertView.findViewById(R.id.tetPing);
			//holder.txtSize = (TextView) convertView.findViewById(R.id.txtSize);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.imgZan
				.setText(new SimpleDateFormat("yyyy-MM-dd").format(Long
						.parseLong(items.getList().get(position)
								.getCreatetime()) * 1000));

		holder.tv_content.setText(items.getList().get(position).getUserNick());
		holder.tv_title.setText(items.getList().get(position).getComment());
		if (items.getList().get(position).getSkuInfo().getSkuDesc() != null) {
			holder.tetPing.setText(items.getList().get(position).getSkuInfo()
					.getSkuDesc());
		} else {
			holder.tetPing.setText("暂未填写");
		}

		XImageLoader
				.load(GlobalConfig.API_URL + "/picture/index?id="
						+ items.getList().get(position).getUserHead(),
						holder.iv_avatar);
		final ArrayList<String> imageUrls = items.getList().get(position)
				.getImg();
		if (imageUrls == null || imageUrls.size() == 0) { // 没有图片资源就隐藏GridView
			holder.gridview.setVisibility(View.GONE);
		} else {
			holder.gridview.setAdapter(new NoScrollGridAdapter(mContext,
					imageUrls));
		}
		// 点击回帖九宫格，查看大图
		holder.gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				imageBrower(position, imageUrls);
			}
		});
		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}

	/**
	 * listview组件复用，防止“卡顿”
	 * 
	 * @author Administrator
	 * 
	 */
	class ViewHolder {
		private ImageView iv_avatar;
		private TextView tv_title;
		private TextView tv_content;
		private NoScrollGridView gridview;

		private TextView imgZan, tetPing, txtSize;
	}
}